﻿using Microsoft.EntityFrameworkCore;
using Simple.EntityFramewokCore.Core;
using Simple.EntityFramewokCore.Core.Base;
using Video.Domain.Users;

namespace Video.EntityFrameworkCore.Users;

public class UserInfoRepository : Repository<VideoDbContext, UserInfo>, IUserInfoRepository
{
    private readonly VideoDbContext _dbContext;

    public UserInfoRepository(VideoDbContext dbContext) : base(dbContext)
    {
        this._dbContext = dbContext;
    }

    public Task<UserInfoRoleView?> GetAsync(Guid id)
    {
        throw new NotImplementedException();
    }



    public async Task<UserInfoRoleView?> GetUserInfoRoleAsync(string username, string password)
    {
        var userInfo = await _dbContext.UserInfo.Where(x => x.UserName == username && x.Password == password)
            .Select(x => new UserInfoRoleView()
            {
                Avatart = x.Avatart,
                CreatedTime = x.CreatedTime,
                Id = x.Id,
                Name = x.Name,
                Password = x.Password,
                UserName = x.UserName
            }).FirstOrDefaultAsync();

        if (userInfo == null)
        {
            return null;
        }

        var query = from role in _dbContext.Role
                    join userRole in _dbContext.UserRole on role.Id equals userRole.RoleId
                    select role;

        userInfo.Role = await query.ToListAsync();
        return userInfo;

    }


    public Task<int> GetCountAsync(string keywords, DateTime? startTime, DateTime? endTime)
    {
        throw new NotImplementedException();
    }

    public Task<List<UserInfo>> GetListAsync(string keywords, DateTime? startTime, DateTime? endTime, int skipCount, int maxResultCount)
    {
        throw new NotImplementedException();
    }

    public async Task DeleteAsync(IEnumerable<Guid> id)
    {
        await _dbContext.Database.ExecuteSqlRawAsync("Delete From UserInfos Where Id IN ({0})", string.Join(',', id));
    }

    public async Task ForbiddenAsync(IEnumerable<Guid> id)
    {
        await _dbContext.Database.ExecuteSqlRawAsync("Update UserInfos Set Enable Where Id IN ({0})", string.Join(',', id));
    }
}
